package com.augurit.gzcgwsp.jzfqwczz.common.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.jzfqwczz.common.convert.YwYnBusinfoHfqkConvertor;
import com.augurit.gzcgwsp.jzfqwczz.common.dao.YwYnBusinfoHfqkDao;
import com.augurit.gzcgwsp.jzfqwczz.common.entity.YwYnBusinfoHfqk;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoHfqkService;
import com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoNssyService;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnBusinfoHfqkForm;
import com.augurit.gzcgwsp.jzfqwczz.common.web.form.YwYnBusinfoNssyForm;
import com.augurit.gzcgwsp.jzfqwczz.entity.YwYnJzfqwczcJbxx;
import com.augurit.gzcgwsp.jzfqwczz.service.IYwYnJzfqwczcJbxxService;
import com.augurit.gzcgwsp.ljzyz.service.IYwYnBusinfoService;
import com.augurit.gzcgwsp.ljzyz.web.form.YwYnBusinfoForm;

@Service
@Transactional
public class YwYnBusinfoHfqkServiceImpl implements IYwYnBusinfoHfqkService {
	@Autowired
	private IYwYnJzfqwczcJbxxService ywYnJzfqwczcJbxxService;
	@Autowired
	private YwYnBusinfoHfqkDao YwYnBusinfoHfqkDao;
	@Autowired
	private IYwYnBusinfoService ywYnBusinfoService;
	@Autowired
	private IYwYnBusinfoNssyService  ywYnBusinfoNssyService;

	@Transactional(readOnly = true)
	public YwYnBusinfoHfqkForm get(Long id) {
		YwYnBusinfoHfqk entity = YwYnBusinfoHfqkDao.get(id);
		return YwYnBusinfoHfqkConvertor.convertVoToForm(entity);
	}
	
	public void delete(Long... ids) {
		YwYnBusinfoHfqkDao.delete(ids);
	}

	public void save(YwYnBusinfoHfqkForm... forms) {
		if(forms != null)
			for(YwYnBusinfoHfqkForm form : forms)
				this.save(form);
	}
	
	public void update(YwYnBusinfoHfqkForm form) {
	    YwYnBusinfoHfqkForm dbform = null;
	    if (form.getId() != null && form.getId() != 0) dbform = this.get(form.getId());
	    
	    if (dbform == null) {
	        this.save(form);
	    } else {
	        new DozerBeanMapper().map(form, dbform);
            this.save(dbform);
	    }
	}
	
	public void save(YwYnBusinfoHfqkForm form){
		
		if(form != null){
			YwYnBusinfoHfqk entity = null;
			if(form.getId() != null && form.getId().intValue()!=0){
				entity = YwYnBusinfoHfqkDao.getYwYnBusinfoHfqk(form.getId());
				if(entity!=null&&entity.getId()==null ){
					entity = new YwYnBusinfoHfqk();
					entity.setId(form.getId());
				}
			}else{
				entity = new YwYnBusinfoHfqk();
			}
			
			YwYnBusinfoHfqkConvertor.convertFormToVo(form, entity);
			YwYnBusinfoHfqkDao.save(entity);
			form.setId(entity.getId());
		}
	}

	@Transactional(readOnly = true)
	public Page<YwYnBusinfoHfqkForm> search(Page<YwYnBusinfoHfqkForm> page, YwYnBusinfoHfqkForm form) {
		Page pg = PageUtils.newPageInstance(page);
		
		StringBuffer hql = new StringBuffer("from YwYnBusinfoHfqk ps where 1=1");
		List values = new ArrayList();
		
		if(form != null){
			if(form.getId() != null){
		    	hql.append(" and ps.getId="+form.getId());
		    }
		    if(form.getBusId() != null){
		    	hql.append(" and ps.busId="+form.getBusId());
		    }
		}
		
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		YwYnBusinfoHfqkDao.findPage(pg, hql.toString(), values);
		
		List<YwYnBusinfoHfqkForm> list = YwYnBusinfoHfqkConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	@Transactional(readOnly = true)
	public List<YwYnBusinfoHfqkForm> search(YwYnBusinfoHfqkForm form) {
		
		StringBuffer hql = new StringBuffer("from YwYnBusinfoHfqk ps where 1=1");
		List values = new ArrayList();		
		if(form != null){
			if(form.getId() != null){
		    	hql.append(" and ps.getId="+form.getId());
		    }
		    if(form.getBusId() != null){
		    	hql.append(" and ps.busId="+form.getBusId());
		    }
		}
		List<YwYnBusinfoHfqk> YwYnBusinfoHfqkList = YwYnBusinfoHfqkDao.find(hql.toString(), values);
		List<YwYnBusinfoHfqkForm> YwYnBusinfoHfqkFormList = YwYnBusinfoHfqkConvertor.convertVoListToFormList(YwYnBusinfoHfqkList);		
		return YwYnBusinfoHfqkFormList;
	}
	
	@Transactional(readOnly = true)
	public Page<YwYnBusinfoHfqkForm> search(Page<YwYnBusinfoHfqkForm> page, List<PropertyFilter> filters) {
		Page<YwYnBusinfoHfqk> pg = PageUtils.newPageInstance(page);
		
		YwYnBusinfoHfqkDao.findPage(pg, filters);
		
		List<YwYnBusinfoHfqkForm> list = YwYnBusinfoHfqkConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}

	public void updateYwYnBusinfoHfqk(YwYnBusinfoHfqkForm form) {
	    YwYnBusinfoHfqkForm dbform = null;
	    if (form.getId() != null && form.getId() != 0)
	    	dbform = this.get(form.getId());
	    if (dbform != null) {
			YwYnBusinfoHfqk entity = null;
			entity = YwYnBusinfoHfqkDao.getYwYnBusinfoHfqk(dbform.getId());
			YwYnBusinfoHfqkConvertor.convertFormToVo(form, entity);
			if(entity!=null){
				YwYnBusinfoHfqkDao.updateYwYnBusinfoHfqk(entity);
			}
	    }
	}

    public void saveEntity(YwYnBusinfoHfqk entity) {
       this.YwYnBusinfoHfqkDao.save(entity);
        
    }

	/**
	 * 保存现场勘察表返回id
	 */
	public Long saveEnterpriseInfo(YwYnBusinfoHfqkForm form) {
		YwYnBusinfoHfqk entity =new YwYnBusinfoHfqk();
		
		YwYnBusinfoHfqkConvertor.convertFormToVo(form, entity);
		
		return YwYnBusinfoHfqkDao.saveEntity(entity);
	}

	@Override
	public void save(YwYnBusinfoHfqkForm form, String flag) {
		if(form.getId()!=null &&form.getId().intValue()==0){
			form.setId(null);
		}
		YwYnBusinfoForm bus=ywYnBusinfoService.get(form.getBusId());
		YwYnBusinfoNssyForm nssy=ywYnBusinfoNssyService.getEntity(String.valueOf(form.getBusId()),"2");
		if(StringUtils.isBlank(flag)){//如果是在核发环节
			if("发证".equals(form.getSffbzp())){
				//发牌进入科审批
				bus.setNssts("3");
				form.setSts("2");//审核完成
			}else{
				//回退到年审审验
				bus.setNssts("1");
				form.setSts("1");
				nssy.setSts("1");
			}
		}else{
			if("ksp".equals(flag)){//科长审批
				if("不同意发证".equals(form.getKspjg())){
					//回退到年审核发
					form.setSts("1");
					bus.setNssts("2");
				}else{
					bus.setNssts("4");
					form.setSts("2");//到副处长审批
				}
			}else if("fgcsp".equals(flag)){//到副处长审批
				if("不同意发证".equals(form.getFgcspjg())){
					//回退到科长
					form.setSts("2");
					bus.setNssts("3");
				}else{
					if(StringUtils.isNotBlank(bus.getIsNewBus())){//如果不是新车需要处长审批
						//结束
						nssy.setSts("0");
						bus.setNssts("0");
						form.setSts("0");
					}else{
						bus.setNssts("5");
						form.setSts("2");//到处长审批
					}
				}
			}else if("csp".equals(flag)){
				if("不同意发证".equals(form.getCspjg())){
					//回退到副处长
					form.setSts("2");
					bus.setNssts("4");
				}else{
					//结束
					nssy.setSts("0");
					bus.setNssts("0");
					bus.setIsNewBus("0");
					form.setSts("0");
					
				}
			}
		}
		ywYnBusinfoService.save(bus);
		ywYnBusinfoNssyService.save(nssy,false);
		save(form);
	}

	@Override
	public YwYnBusinfoHfqkForm getEntity(Long id, String sts) {
		String sql=" from YwYnBusinfoHfqk where busId="+id+" and sts='"+sts+"'";
		List<YwYnBusinfoHfqk> list=YwYnBusinfoHfqkDao.find(sql);
		YwYnBusinfoHfqkForm form=new YwYnBusinfoHfqkForm();
		if(list.size()>0){
			YwYnBusinfoHfqk entity=list.get(0);
			form=YwYnBusinfoHfqkConvertor.convertVoToForm(entity);
		}
		
		return form;
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoHfqkService#getHfqkByBusId(java.lang.Long, java.lang.String)
	 */
	@Override
	public List<YwYnBusinfoHfqkForm> getHfqkByBusId(Long id, String curYear) {
		String sql="";
		if(StringUtils.isNotBlank(curYear)){
			sql=" from YwYnBusinfoHfqk where busId="+id+" and sts='0' and to_char(yxksrq,'yyyy')='"+curYear+"'";
		}else{
			sql=" from YwYnBusinfoHfqk where busId="+id+" and sts='0' ";
		}
		sql+=" order by yxjsrq asc";
		List<YwYnBusinfoHfqk> list=this.YwYnBusinfoHfqkDao.find(sql);
		List<YwYnBusinfoHfqkForm> forms=new ArrayList<YwYnBusinfoHfqkForm>();
		for(YwYnBusinfoHfqk entity:list){
			YwYnBusinfoHfqkForm form=new YwYnBusinfoHfqkForm();
			form=YwYnBusinfoHfqkConvertor.convertVoToForm(entity);
			forms.add(form);
		}
		return forms;
	}

	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.jzfqwczz.common.service.IYwYnBusinfoHfqkService#saveAllHfqk(java.lang.String, java.lang.String)
	 */
	@Override
	public void saveAllHfqk(String ids, String flag,String curUser,String checkDate,String startDate,String endDate) {
		String[] idsArr=ids.split(",");
		String sts="1";
		if(StringUtils.isNotBlank(flag)){
			sts="2";
		}
		
		for(String busId:idsArr){
			YwYnBusinfoNssyForm nssy=this.ywYnBusinfoNssyService.getEntity(String.valueOf(busId),"2");
			YwYnBusinfoForm bus=this.ywYnBusinfoService.get(Long.valueOf(busId));
			YwYnBusinfoHfqkForm form=getEntity(Long.valueOf(busId),sts);
			form.setBusId(Long.valueOf(busId));
			form.setNssyId(nssy.getId());
			if(StringUtils.isNotBlank(startDate)){
				form.setYxksrq(DateUtilsEX.getStringToDate(startDate, "yyyy-MM-dd"));
			}
			if(StringUtils.isNotBlank(endDate)){
				form.setYxjsrq(DateUtilsEX.getStringToDate(endDate, "yyyy-MM-dd"));
			}
			if(StringUtils.isNotBlank(checkDate)){
				form.setJcrq(DateUtilsEX.getStringToDate(checkDate, "yyyy-MM-dd"));
			}
			
			
			//设置默认的有效开始和结束时间
			if(form.getYxksrq()==null){
				form.setYxksrq(new Date());
			}
			if(form.getYxjsrq()==null){
				Date date=DateUtilsEX.getDateByMonth(form.getYxksrq(), 6,true);
				form.setYxjsrq(date);
			}
			if(StringUtils.isBlank(flag)){//核发的时候
				form.setSffbzp("发证");
				if(form.getJyy()==null){
					form.setJyy(curUser);
				}
			}else if("ksp".equals(flag)){
				 //科审批
				if(form.getKspr()==null){
					form.setKspr(curUser);
				}
				//if(form.getKspjg()==null){
					form.setKspjg("同意发证");
				//}
				//if(form.getKspyj()==null){
					form.setKspyj("同意");
				//}
				
			}else if("fgcsp".equals(flag)){
				 //副处长审批
				//if(form.getFgcspr()==null){
					form.setFgcspr(curUser);
				//}
				//if(form.getFgcspjg()==null){
					form.setFgcspjg("同意发证");
				//}
				//if(form.getFgcspyj()==null){
					form.setFgcspyj("同意");
				//}
			}else if("csp".equals(flag)){
				 //处长审批
				//if(form.getCspr()==null){
					form.setCspr(curUser);
				//}
				//if(form.getCspjg()==null){
					form.setCspjg("同意发证");
				//}
				//if(form.getCspyj()==null){
					form.setCspyj("同意");
				//}
			}
			if(form.getXbzh()==null){
				form.setXbzh(bus.getBzh());
			}
			if(form.getXclbh()==null){
				form.setXclbh(bus.getCphm());
			}
			if(form.getBzplsh()==null){
				form.setBzplsh(bus.getBzplsh());
			}
			
			save(form,flag);
 	 }
	}

	@Override
	public Date getCzzDate(String busId) {
		YwYnBusinfoForm busForm=ywYnBusinfoService.get(Long.valueOf(busId));
		if(busForm!=null){
			YwYnJzfqwczcJbxx jbxx=ywYnJzfqwczcJbxxService.getValidEndtimeEntity(busForm.getQyid());
			return jbxx.getValidEndtime();
		}
	
		return null;
	}

	@Override
	public YwYnBusinfoHfqk getNewHfqkByBusId(Long id) {
		 String sql=" from YwYnBusinfoHfqk where busId="+id+" and sts='0' and yxksrq is not null order by id desc";
		 List list=this.YwYnBusinfoHfqkDao.find(sql);
		 if(list!=null){
			 return (YwYnBusinfoHfqk) list.get(0);
		 }
		return null;
	}

	@Override
	public void updateEntity(YwYnBusinfoHfqk hfqk) {
		this.YwYnBusinfoHfqkDao.save(hfqk);
		
	}
}
